﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using YmtAuth.Domain.Model.Safe;
using YmtAuth.Domain.Repository;

namespace YmtAuth.Repository.MySQL
{
    public class DeviceTokenAuthRepository : MySQLRepository<DeviceTokenAuthEntity>, IDeviceTokenAuthRepository
    {
        public DeviceTokenAuthRepository()
            : base(ConfigurationManager.AppSettings["UserDeviceTokenAuthHistory_MySqlConnection"], "UserDeviceTokenAuthHistory")
        {

        }

        public DeviceTokenAuthEntity FindLastDeviceTokenAuth(int userId, string devicerId, byte type = 0)
        {
            var sql =
                "select `UserId`,`DevicerId`,`Type`,`LastAuthTime`,`CreatDateTime`  from UserDeviceTokenAuthHistory where `UserId`=@UserId AND `DevicerId`=@DevicerId order by LastAuthTime desc limit 1";           
            var par = this.Parameters.AddInt("@UserId", userId).AddString("@DevicerId", devicerId).Build();
           
            return Query(sql, par, 30, CommandType.Text).FirstOrDefault();
            
        }
    }
}
